//
//  WYChannelView.m
//  网易新闻19
//  频道视图
//  Created by heima on 16/8/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "WYChannelView.h"
#import "WYChannelModel.h"
#import "WYChannelLabel.h"

@interface WYChannelView()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation WYChannelView

+ (instancetype)channelView {
    return [[[NSBundle mainBundle] loadNibNamed:@"WYChannelView" owner:nil options:nil] lastObject];
}

/**
 *  重写set方法,根据频道的数据添加子控件
 *
 */
- (void)setChannels:(NSArray *)channels {
    _channels = channels;
    
    // 每个label之前的间距
    CGFloat margin = 20;
    CGFloat x = margin;
    
    for (int i=0; i<channels.count; i++) {
        
        // 取到频道的模型
        WYChannelModel *model = [channels objectAtIndex:i];
        // 初始化label
        WYChannelLabel *label = [WYChannelLabel labelWithModel: model];
        // UILabel *label = [UILabel labelWithText:model.tname andTextColor:[UIColor blackColor] andFontSize:14];
        // 设置label的frame
        label.frame = CGRectMake(x, 0, label.frame.size.width, 35);
        // 将label添加到scrollView中
        [self.scrollView addSubview:label];
        // 将x递增
        x += label.frame.size.width + margin;
        
        // 添加一个tap的手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        // 将手势 添加到label上
        [label addGestureRecognizer:tap];
        
        // 开启交互
        label.userInteractionEnabled = true;
    }
    // 为了让其滚动
    [self.scrollView setContentSize:CGSizeMake(x, 0)];
    
    // 给第0个lable设置成红色的
    [self setScale:1 forIndex:0];
}

#pragma mark - 手势的监听事件
- (void)tapGesture:(UITapGestureRecognizer *)ges {
    // ges.view 代表是该手势添加到的那个一个view
    NSLog(@"%@", ges.view);
    
    // 调用代理方法,通过外界,当前哪一个频道的label点击了
    if ([self.delegate respondsToSelector:@selector(channelView:clickWithIndex:)]) {
        [self.delegate channelView:self clickWithIndex:[self.scrollView.subviews indexOfObject:ges.view]];
    }
    
    for (WYChannelLabel *label in self.scrollView.subviews) {
        // 要让当前点击的label的scale变成1
        // 让其他的label的scale变成0
        // bool 值本质上就是0和1
        if (ges.view == label) {
            // 当前点击的label
            label.scale = 1;
        }else{
            label.scale = 0;
        }
        // label.scale = ges.view == label;
    }
}

- (void)setScale:(CGFloat)scale forIndex:(NSInteger)index {
    // 1. 取到对应index的label
    WYChannelLabel *label = [self.scrollView.subviews objectAtIndex:index];
    // 设置缩放比例
    NSLog(@"%@", [label class]);
    label.scale = scale;
    
    if (scale == 1) {
        // 将该index对应的label移动到中间
        // 滚动的距离 = label的中心点x - 当前scrollView的宽度的一半
        CGFloat offsetX = label.center.x - self.scrollView.frame.size.width * 0.5;
        // 定义最小的偏移量
        CGFloat minOffset = 0;
        CGFloat maxOffset = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
        // 如果比最小的偏移量还小的话
        if (offsetX < minOffset) {
            // 设置成最小的
            offsetX = 0;
        }else if (offsetX > maxOffset) {
            // 如果大于最大值,那么就设置成最大值
            offsetX = maxOffset;
        }
        [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:true];
    
    }
    
}

@end
